// Script for NiftyPlayer 1.7, by tvst from varal.org
// Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
//modify by 李荣平 QQ：985867294
 var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/(mozilla\/5.0).*msie ([\d.]+)/)) ? Sys.ie9 = s[2] :
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
	},
	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};

function niftyplayer(name)
{
	this.obj = FlashHelper.getMovie(name);
	
	
	this.isMp3Loaded=function(){
		if (!FlashHelper.movieIsLoaded(this.obj)){
			return false;
		}else{
			return true;
		}
	}
	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');
		this.obj.SetVariable(eventName, action);
	};
	//return this;
}


function bgsound(id,src,autoplay,path){
	if(typeof path=="undefined"){
		path="";
	}
	var parent=document.getElementById(id);
	if(Sys.ie9||Sys.chrome){
		if(autoplay==1){
			var str='<audio autoplay id="niftyPlayer1" src="'+src+'" controls ></audio>';
		}else{
			var str='<audio preload id="niftyPlayer1" src="'+src+'" controls ></audio>';
		}
		parent.innerHTML=str;
		this.player=document.getElementById("niftyPlayer1");
	}else{
		var arr=['<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="38" id="niftyPlayer1" align="">',
		         '<param name=movie value="'+path+'niftyplayer.swf?file='+src+'&as='+autoplay+'">',
		         '<param name=quality value=high>',
		         '<param name=bgcolor value=#FFFFFF>',
		         '<embed src="'+path+'niftyplayer.swf?file='+src+'&as='+autoplay+'" quality=high bgcolor=#FFFFFF width="165" height="38" name="niftyPlayer1" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">',
		         '</embed></object>'];
		var str=arr.join(" ");
		parent.innerHTML=str;
		this.player=new niftyplayer("niftyPlayer1");
	}
	this.play=function(){
		this.player.play();
	},
	this.pause=function(){
		this.player.pause();
	},
	this.stop=function(){
		if(Sys.ie9||Sys.chrome){
			this.player.currentTime =0;
			this.pause();
		}else{
			this.player.stop();
		}
	},
	this.load=function(url){
		this.stop();
		if(Sys.ie9||Sys.chrome){
			this.player.src=url;
			
		}else{
			this.player.load(url);
		}
		
	},
	this.loadPlay=function(url){
		this.stop();
		if(Sys.ie9||Sys.chrome){
			this.player.src=url;
			this.player.autoplay="autoplay";
		}else{
			this.player.load(url);
			this.play();
		}
	},
	this.loop=function(elem){
		if(Sys.ie9||Sys.chrome){
			this.player.loop="loop";
		}else{
			var that=this.player;
			var state_timer=setInterval(function(){
				var state=that.getLoadingState();
				if(state!="empty"){
					that.registerEvent('onSongOver',elem+'.play();');
					clearInterval(state_timer);
				}
			},500);
			
		}
	},
	this.playone=function(time){
		if(Sys.ie9||Sys.chrome){
			this.stop();
			this.play();
		}else{
			this.play();
		}
	}
}
